new Vue({
    el: '#mainBody',
    data: function () {
        return {
            navList: [],
            allMenus: [],
            hotMenus: [
		            {
		            	"id": "XBT.data.staff",
		            	"icon": "icofont icofont-user-search",
		            	"text": "内勤人力管理",
		            	"classs": null,
		            	"type": "MENU",
		            	"url": "/jump/dataupload/staff"
		            },
		            {
		            	"id": "XBT.data.datareport",
		            	"icon": "icofont icofont-chart-flow",
		            	"text": "数据上报管理",
		            	"classs": null,
		            	"type": "MENU",
		            	"url": "/jump/datareport/dataReport"
		            },
		            {
		            	"id": "XBT.data.datatype",
		            	"icon": "icofont icofont-gears",
		            	"text": "收集类型管理",
		            	"classs": null,
		            	"type": "MENU",
		            	"url": "/jump/datatype/dataType"
		            }
            ],
            productItems : [], // 产品资料列表
            searchFormProduct : { // 查询条件
            	productcode : '',
            	productname : '',
            	insurancetype : '',
            	productstate : '',
            	atttype : '',
            	releasestate : '',
            	createname : '',
            	createtime : '',
            	ident : '',
            	releasecolumn : '',
            	viewabledept : '',
            	viewablepsn : '',
            	examineState : ''
            },
            projectItems : [ // 项目活动列表
            ],
            systemUrl:{
            	hrpmsUrl:'',
            	upsUrl:'',
            	uamsUrl:'',
            	riUrl:'',
            	rdmUrl:'',
            	rrsUrl:'',
            	crmUrl:'',
            	mbUrl:'',
            	mbmsUrl:'',
            	xytUrl:''
            },
            searchFormProject : { // 查询条件
				projectname : '',
				projectstartdate : '',
				releasestate : '',
				createname : '',
				createtime : '',
				atttype : '',
				examineState : '',
				ident : 0
			}
        }
    },
    methods: {
        openUrl: function (url,id) {
	        var obj = JSON.parse(localStorage.getItem('hotMenusCount061501')) || {}
	        //for (var i = 0; i < this.arrayObject.length; i++) {
	        //var dt = this.arrayObject[i];
	        if(obj[id]) {
	            obj[id]++
	        } else {
	            obj[id] = 1
	        }
	        //}
	        //console.log(obj);
	        localStorage.setItem('hotMenusCount061501',JSON.stringify(obj));
	        setTimeout(function(){
	        	if(url){
	        		location.href=url;
	            }
	        },500)
        },
        managementAuthority: function(navList){
        	var count = 0;
        	for(var i=0; i<navList.length; i++){
        		if(navList[i].text=="产品管理"){
        			count+=1;
        		}
        	}
        	if(count == 1){
        		$("#producted")[0].style.display = 'block'; 
        		$("#activityed")[0].style.display = 'block'; 
        	}
        },
        /**
         * 点击的是产品管理列表
         * @param product
         */
        clickProductLi: function(product){
            // 0：产品，1产品组合
            var identName = "product";
            if(product.ident == "1"){
                identName = "sale";
            }
            sessionStorage.setItem("cacheVal",JSON.stringify(product));
            window.location.href = '/jump/product/productItems#'+identName;
        },
        /**
         * 点击的是项目/活动管理列表
         * @param project
         */
        clickProjectLi: function(project){
            sessionStorage.setItem("cacheVal",JSON.stringify(project));
            window.location.href = '/jump/product/productItems#activity';
        }
    },
    created: function () {
        var _this = this
        $("#producted")[0].style.display = 'none'; 
		$("#activityed")[0].style.display = 'none'; 
        $.ajax({
            url: '/findMenuList',
            type: 'get',
            dataType: 'json',
            success: function(res) {
                _this.navList = res.data.rows
                _this.managementAuthority(_this.navList);
                var resDataRowsLength =  res.data.rows.length
            	for(var i=0; i<resDataRowsLength; i++){
            		_this.allMenus = _this.allMenus.concat(res.data.rows[i].children)
            	}
            },
            error: function(err) {
            	if(err.responseJSON.message=="session clean"){
                    errorMessage(_this.$message, '登陆已失效重新登陆!');
                    toLogin();
                    return;
                }
                console.log(err)
            }
        })
        /**
          concat hotMenus
         */
        setTimeout(function(){

        	var clickCountLocal = localStorage.getItem("hotMenusCount061501");
        	if(clickCountLocal!=null || clickCountLocal!=undefined) {
            var arr = _this.allMenus.filter(function (item) {
                var myitem = ''
                clickCountLocalSort.filter(function (value) {
                    if (item.id == value) {
                        myitem = value
                    }
                })
                return myitem
            })
            var newArr = []
            clickCountLocalSlice.forEach(function(item) {
                arr.forEach(function(value){
                    if(value.id == item){
                        newArr.push(value)
                    }
                })
            })
            _this.hotMenus= newArr.concat(_this.allMenus)
        }
        _this.hotMenus = uniqueArray(_this.hotMenus,'id')
        var cssArr = ["bg-nth4","bg-first","bg-nth3","bg-nth2"];
        for(var i=0; i < _this.hotMenus.length; i++){
        	if(_this.hotMenus[i]){
        		_this.$set(_this.hotMenus[i], 'css', cssArr[i])
        	}
        }
        console.log(_this.hotMenus)
        },1000)

        //加载产品列表
        $.ajax({
			url : '/product/findProduct',
			type : 'post',
			data : {
				"data" : JSON.stringify(_this.searchFormProduct)
			},
			dataType : 'json',
			success : function(res) {
				_this.productItems = res.data.beanList
			},
			error : function(err) {
				if(err.responseJSON.message=="session clean"){
                    errorMessage(_this.$message, '登陆已失效重新登陆!');
                    toLogin();
                    return;
                }
                if(err.status == 403){
                    errorMessage(_this.$message, "没有权限，请先申请该权限！");
                    return;
                }
				errorMessage(_this.$message, '获取产品列表出错！')
			}
		})
		//加载项目活动列表
		$.ajax({
			url : '/project/findProject',
			type : 'post',
			data : {
				"data" : JSON.stringify(_this.searchFormProject)
			},
			dataType : 'json',
			success : function(res) {
				_this.projectItems = res.data.beanList
			},
			error : function(err) {
				if(err.responseJSON.message=="session clean"){
                    errorMessage(_this.$message, '登陆已失效重新登陆!');
                    toLogin();
                    return;
                }
                if(err.status == 403){
                    errorMessage(_this.$message, "没有权限，请先申请该权限！");
                    return;
                }
				errorMessage(_this.$message, '获取项目/活动列表出错！')
			}
		})
		
		//加载系统列表
		$.ajax({
			url : '/index/systemUrls',
			type : 'post',
			dataType : 'json',
			success : function(res) {
				_this.systemUrl.hrpmsUrl = res.data.hrpmsUrl
				_this.systemUrl.upsUrl = res.data.upsUrl
				_this.systemUrl.uamsUrl = res.data.uamsUrl
				_this.systemUrl.riUrl = res.data.riUrl
				_this.systemUrl.rdmUrl = res.data.rdmUrl
				_this.systemUrl.rrsUrl = res.data.rrsUrl
				_this.systemUrl.crmUrl = res.data.crmUrl
				_this.systemUrl.mbUrl = res.data.mbUrl
				_this.systemUrl.mbmsUrl = res.data.mbmsUrl
				_this.systemUrl.xytUrl = res.data.xytUrl
			},
			error : function(err) {
				if(err.responseJSON.message=="session clean"){
                    errorMessage(_this.$message, '登陆已失效重新登陆!');
                    toLogin();
                    return;
                }
                if(err.status == 403){
                    errorMessage(_this.$message, "没有权限，请先申请该权限！");
                    return;
                }
				errorMessage(_this.$message, '获取系统列表出错！')
			}
		})
    }
})

$(function() {// 初始化内容
});

/**
 JSON数组去重
 */
function uniqueArray(array, key){
	if(array.length==0){
		return [];
	}
    var result = [array[0]];
    for(var i = 1; i < array.length; i++){
        var item = array[i];
        var repeat = false;
        for (var j = 0; j < result.length; j++) {
            if (item[key] == result[j][key]) {
                repeat = true;
                break;
            }
        }
        if (!repeat) {
            result.push(item);
        }
    }
    return result;
}

/**
 * 取出localStorage的点击次数，并排序
 */
//localStorage.clear();
var clickCountLocalSlice;
var clickCountLocalSort;
var clickCountLocal;
$.ajax({
    url: '/HotMenus/findHotMenus',
    type: 'get',
    //data: {'userid':'111'},
    dataType: 'json',
    success: function(res) {
    	var data = res.data;
    	 var obj = {}
    	 for(var i =0;i<data.length;i++){
    		 var id = data[i].menuid;
    		 var hotPoint = data[i].hotpoint;
             obj[id]=hotPoint;
    	 }
         //console.log(obj);
         localStorage.setItem('hotMenusCount061501',JSON.stringify(obj));
          clickCountLocal = localStorage.getItem("hotMenusCount061501");
         if(clickCountLocal!=null || clickCountLocal!=undefined) {
             clickCountLocal = JSON.parse(localStorage.getItem("hotMenusCount061501"));
             clickCountLocalSort = Object.keys(clickCountLocal).sort(function(a,b){
                 return clickCountLocal[b] - clickCountLocal[a]
             })
             clickCountLocalSlice = clickCountLocalSort.slice(0,4)
             console.log(clickCountLocalSort)
         }
    }
});
